<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/09 16:52
 * Blog：www.myblogs.xyz
 */

namespace app\api\controller;


use app\common\model\Member;
use app\common\model\Verify;

class Login extends Common
{
    // 短信验证码登录
    public function into()
    {
        $this->require = ['account', 'verify_code'];
        $this->selectable = [
            'code', 'auth_code', 'device_number', 'app_type',
            'app_version','device_type', 'device_version'
        ];
        $this->verifyParam();
        if (empty($this->data['account'])){
            $this->jsonError('请输入手机号');
        }
        $verify_model = new Verify();
        if (!$verify_model->VerifyCode($this->data, 1)){
            $this->jsonError($verify_model->getError());
        }
        unset($this->data['verify_code']);
        $list_where = [];
        if (is_mobile($this->data['account'])){
            $list_where['mobile'] = $this->data['account'];
        }
        if (is_email($this->data['account'])){
            $list_where['email'] = $this->data['account'];
        }
        if (empty($list_where)){
            $this->jsonError('login_failure');
        }
        $member_model = new Member();
        if (!empty($this->data['code'])){
            // verify auth
            $auth_data = $member_model->getWxAuth($this->data['code']);
            if (!$auth_data){
                $this->jsonError('auth_error');
            }
            // verify is register
            if (!$member_model->getExists($list_where)){
                $save_data = [
                    'mobile'=>$this->data['account'],
                    'openid'=>$auth_data['openid']
                ];
                if (!$member_model->memberRegister($save_data)){
                    $this->jsonError('login_error');
                }
            }else{
                $member_data = $member_model->getFind($list_where, 'member_id,openid');
                if (!empty($member_data['member_id']) && empty($member_data['openid'])){
                    $member_model->where('member_id', $member_data['member_id'])->update(['openid'=>$auth_data['openid']]);
                }else{
                    if ($member_data['openid'] != $auth_data['openid']){
                        $this->jsonError('account_error');
                    }
                }
            }
        }else{
            if (!$member_model->getExists($list_where)){
                if (!$member_model->memberRegister(['mobile'=>$this->data['account']])){
                    $this->jsonError('login_error');
                }
            }
        }
        $data = $member_model->memberLogin($this->data);
        if (!$data){
            $this->jsonError($member_model->getError());
        }
        if (!filter_var($data['face'], FILTER_VALIDATE_URL)){
            $data['face'] = $this->facePath . $data['face'];
        }

        $this->jsonResult($data);
    }

    // 微信已经授权登录
    public function wxApplet()
    {
        $this->require = ['code', 'encryptedData', 'iv'];
        $this->selectable = ['app_type', 'app_version','device_type', 'device_number'];
        $this->verifyParam();
        // 获取微信授权信息
        $member_model = new Member();
        $auth_data = $member_model->getWxAuth($this->data['code']);
        if (!$auth_data){
            $this->jsonError('授权失败');
        }
        //  获取微信授权手机号
        if (empty($this->data['encryptedData']) || empty($this->data['iv'])){
            $this->jsonError('授权失败');
        }
        $this->data['session_key'] = $auth_data['session_key'];
        $json_data = $member_model->verifyWxMobile($this->data);
        if (empty($json_data)){
            $this->jsonError('授权失败');
        }
        $mobile_data = json_decode($json_data, true);
        if (empty($mobile_data['phoneNumber'])){
            $this->jsonError('授权失败');
        }
        if (!$member_model->getExists(['mobile'=>$mobile_data['phoneNumber']])){
            $save_data = [
                'mobile'=>$mobile_data['phoneNumber'],
                'openid'=>$auth_data['openid'],
                'wx_session_key'=>$auth_data['session_key']
            ];
            if (!$member_model->memberRegister($save_data)){
                $this->jsonError('登录失败，请稍后重试');
            }
        }
        $data = $member_model->memberLogin(['account'=>$mobile_data['phoneNumber']]);
        if (!$data){
            $this->jsonError($member_model->getError());
        }
        $member_model->where('member_id', $data['member_id'])->update([
            'openid'=>$auth_data['openid'],
            'wx_session_key'=>$auth_data['session_key']
        ]);
        if (!filter_var($data['face'], FILTER_VALIDATE_URL)){
            $data['face'] = $this->facePath . $data['face'];
        }

        $this->jsonResult($data);
    }

}